﻿using System;
using System.Runtime.InteropServices;

namespace FileSC.Win.api
{
    public class Win32Api
    {
        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        //安装钩子
        [DllImport("user32.dll", ExactSpelling = true, EntryPoint = "SetWindowsHookExA", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        //卸载钩子
        [DllImport("user32.dll", ExactSpelling = true, EntryPoint = "UnhookWindowsHookEx", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
        //调用下一个钩子
        [DllImport("user32.dll", ExactSpelling = true, EntryPoint = "CallNextHookEx", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        public delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
        [DllImport("user32.dll", ExactSpelling = true, EntryPoint = "SetWinEventHook", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
         hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
         uint idThread, uint dwFlags);
        [DllImport("user32.dll", ExactSpelling = true, EntryPoint = "UnhookWinEvent", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWinEvent(IntPtr hWinEventHook);

        [DllImport("kernel32.dll", ExactSpelling = true, EntryPoint = "GetModuleHandleA", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr GetModuleHandle(string lpModuleName);

        [DllImport("kernel32.dll", ExactSpelling = true, EntryPoint = "SetProcessWorkingSetSize", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);


        [DllImport("user32.dll", ExactSpelling = true, EntryPoint = "GetDC", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr GetDC(IntPtr ptr);
        [DllImport("gdi32.dll", ExactSpelling = true, EntryPoint = "GetDeviceCaps", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
        [DllImport("user32.dll", ExactSpelling = true, EntryPoint = "ReleaseDC", CharSet = CharSet.None, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);

        /// <summary>
        /// 释放内存资源
        /// </summary>
        public static void ReleaseWorkingSize()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT) SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
        }
    }
}
